#!/usr/bin/python3

import sys
import os
import docx


def get_doc_list(docs_path):
    files = []
    # 从文件路径获取文件列表,并剔除非doc格式的文件
    for file in os.listdir(docs_path):
        if file.endswith('.doc') or file.endswith('.docx'):
            files.append(docs_path + "/" + file)
    return files


def replace_str(files_list):
    old_str = r"This notice is provided on behalf of Huawei Technologies Co. Ltd. and any of its local subsidiaries which may have provided this product to you in your local country."
    new_str = r"This notice is provided on behalf of openEuler project which is incubated and hosted by the OpenAtom Foundation, and may have provided this product to you in your local country."

    for file in files_list:
        # 创建 Document 对象，相当于打开一个 word 文档
        doc = docx.Document(file)
        for paragraph in doc.paragraphs:
            if old_str in paragraph.text:
                text =  paragraph.text.replace(old_str, new_str)
                # 保存原段落格式
                style = paragraph.style

                paragraph.text = text
                paragraph.style = style
                paragraph.style.font.name = 'Arial'

        doc.save(file)

def main():
    if len(sys.argv) != 2:
        print("ERRRO: Wrong number of parameters")
        sys.exit(1)
    docs_path = sys.argv[1]
    files_list = get_doc_list(docs_path)
    replace_str(files_list)


if __name__=="__main__":
    main()
